package com.shenmazong.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.dto.TbCompany;
import com.shenmazong.dto.TbJob;
import com.shenmazong.mapper.TbJobMapper;
import com.shenmazong.service.TbCompanyService;
import com.shenmazong.mapper.TbCompanyMapper;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.BatchChooseIdVo;
import com.shenmazong.vo.JobVo;
import com.shenmazong.vo.ListParamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class TbCompanyServiceImpl extends ServiceImpl<TbCompanyMapper, TbCompany>
implements TbCompanyService{
    @Autowired
    private TbCompanyMapper tbCompanyMapper;
    @Autowired
    private TbJobMapper tbJobMapper;
    @Override
    public ResultResponse findAllCompanyStudent(ListParamVo listParamVo) {
        Page<TbCompany> page=tbCompanyMapper.findAllCompanyStudent(new Page<>(listParamVo.getCurrent(),listParamVo.getSize()),listParamVo.getParamMap());
        return ResultResponse.SUCCESS(page);
    }

    @Override
    public ResultResponse saveCompany(TbCompany tbCompany) {
        try {
           tbCompanyMapper.insert(tbCompany);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse updateCompany(TbCompany tbCompany) {
        try {
            TbCompany company = tbCompanyMapper.selectById(tbCompany.getCompanyId());
            if (!StringUtils.isEmpty(tbCompany.getCompanyName())){
                company.setCompanyName(tbCompany.getCompanyName());
            }
            if (!StringUtils.isEmpty(tbCompany.getCompanyAddress())){
                company.setCompanyAddress(tbCompany.getCompanyAddress());
            }
            if (!StringUtils.isEmpty(tbCompany.getCompanyImg())){
                company.setCompanyImg(tbCompany.getCompanyImg());
            }
            if (!StringUtils.isEmpty(tbCompany.getCompanyTel())){
                company.setCompanyTel(tbCompany.getCompanyTel());
            }
            if (!StringUtils.isEmpty(tbCompany.getCompanyRecommend())){
                company.setCompanyRecommend(tbCompany.getCompanyRecommend());
            }
            if (!StringUtils.isEmpty(tbCompany.getCompanyUrl())){
                company.setCompanyUrl(tbCompany.getCompanyUrl());
            }
            tbCompanyMapper.updateById(tbCompany);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse deleteCompany(BatchChooseIdVo batchChooseIdVo) {
        if (batchChooseIdVo.getIds().size()==0){
            return ResultResponse.FAILED("至少选择一个哟");
        }
        try {
            for (Long id : batchChooseIdVo.getIds()) {
                TbCompany tbCompany = tbCompanyMapper.selectById(id);
                tbCompany.setDeleteFlag((byte) 1);
                tbCompanyMapper.updateById(tbCompany);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse getJob(BatchChooseIdVo batchChooseIdVo) {
        if (batchChooseIdVo.getIds().size()!=1){
            return ResultResponse.FAILED("非法参数");
        }
        List<JobVo> list= tbCompanyMapper.getJob(batchChooseIdVo.getIds().get(0));
        return ResultResponse.SUCCESS(list);
    }
}




